package com.hejjon.knowledgebase.util;

import com.hejjon.knowledgebase.exception.BusinessException;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author:
 * @date: 2022-12-06 11:44:56
 * @since: 1.0
 */
public class MD5Util {


    public static final String KEY_MD5 = "MD5";

    /***
     * MD5加密（生成唯一的MD5值）
     *
     * @return
     * @throws Exception
     */
    public static String encry(String str) throws Exception {
        MessageDigest md5 = MessageDigest.getInstance(KEY_MD5);
        md5.update(str.getBytes());
        byte[] bytes = md5.digest();
        return new BigInteger(1, bytes).toString(16);
    }


    public static MessageDigest getMessageDigest() {
        try {
            return MessageDigest.getInstance(KEY_MD5);
        } catch (NoSuchAlgorithmException e) {
            throw new BusinessException(e.getMessage());
        }
    }



}
